package com.yypx.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.yypx.dao.PersistentGenericDao;
import com.yypx.entity.BaseObject;

public abstract class PersistentGenericDaoImpl<T extends BaseObject, PK>
		extends SqlMapClientDaoSupport implements PersistentGenericDao<T, PK> {
	private Class<T> cls;
	protected Log logger = null;

	@SuppressWarnings("unchecked")
	public PersistentGenericDaoImpl() {
		this.cls = null;
		Class<?> c = getClass();
		Type t = c.getGenericSuperclass();
		if (t instanceof ParameterizedType) {
			Type[] p = ((ParameterizedType) t).getActualTypeArguments();
			this.cls = (Class<T>) p[0];
		}
		//初始化logger
		logger = LogFactory.getLog(getClass().getName());
	}

	/**
	 * 通过id查询
	 * 
	 * 
	 * @param id
	 *            主键ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public T findById(PK id) {
		try {
			T t = (T) getSqlMapClientTemplate().queryForObject(
					cls.getSimpleName().toString() + ".findById", id);
			return t;
		} catch (Exception e) {
			logger.error("根据id查询出错:" + e.getMessage());
		}
		return null;
	}
	
	@Override
	public void stopuse(PK id) {
		getSqlMapClientTemplate().update(
				cls.getSimpleName().toString() + ".stopuse", id);
	}

	/**
	 * s 检索所有记录
	 * 
	 * s *
	 * 
	 * @return 返回Page对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> selAll() {
		List<T> list = (List<T>) getSqlMapClientTemplate().queryForList(
				cls.getSimpleName().toString() + ".selAll");
		return list;
	}
	
	/**
	 * 返回某联盟下的信息
	 */
	@Override
	public List<T> selAll(UUID consid) {
		List<T> list = (List<T>) getSqlMapClientTemplate().queryForList(
				cls.getSimpleName().toString() + ".selAll",consid);
		return list;
	}

	/**
	 * s 检索所有记录
	 * 
	 * s *
	 * 
	 * @return 返回Page对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<T> selAllByCondition(Map map) {
		List<T> list = (List<T>) getSqlMapClientTemplate().queryForList(
				cls.getSimpleName().toString() + ".selAllByCondition", map);
		return list;
	}

	/**
	 * s 检索所有记录
	 * 
	 * s *
	 * 
	 * @return 返回Page对象
	 */
	@Override
	public int selCountByCondition(Map map) {
		int result = 0;
		Object countObject = getSqlMapClientTemplate().queryForObject(
				cls.getSimpleName().toString() + ".selCountByCondition", map);
		if (countObject == null)
			result = 0;
		else
			result = Integer.parseInt(countObject.toString());
		return result;
	}

	/**
	 * 添加新记录
	 * 
	 * 
	 * @param o
	 *            要添加的新对象
	 * @return 添加后的新对象
	 */
	@Override
	public T addNew(T o) {
		getSqlMapClientTemplate().insert(
				cls.getSimpleName().toString() + ".addNew", o);
		return o;
	}

	/**
	 * 更新记录
	 * 
	 * 
	 * @param o
	 *            要更新的对象
	 * @return 更新后的对象
	 */
	@Override
	public T update(T o) {
		getSqlMapClientTemplate().update(
				cls.getSimpleName().toString() + ".update", o);
		return o;
	}

	/**
	 * 删除记录
	 * 
	 * @param id
	 *            主键ID
	 */
	@Override
	public void delById(PK id) {
		getSqlMapClientTemplate().delete(
				cls.getSimpleName().toString() + ".delById", id);
	}

	/**
	 * 执行存储过程
	 * 
	 * 
	 * @param o
	 *            要更新的对象
	 * @return 更新后的对象
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PK executionStoredProcedureByAdd(Map map) {
		PK id = (PK) getSqlMapClientTemplate().queryForObject(
				cls.getSimpleName().toString() + ".storedProcedure_add", map);
		return id;
	}

	/**
	 * 升 降
	 * 
	 * 
	 * @param id
	 *            type NO type=1升type=0降
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public T upTheNo(int id, int type) {
		if (type == 1) {
			T t = (T) getSqlMapClientTemplate().queryForObject(
					cls.getSimpleName().toString() + ".upDesc", id);
			if (t != null) {
				return t;
			} else {
				T o = (T) getSqlMapClientTemplate().queryForObject(
						cls.getSimpleName().toString() + ".upAsc", id);
				return o;
			}
		} else {
			T t = (T) getSqlMapClientTemplate().queryForObject(
					cls.getSimpleName().toString() + ".upAsc", id);
			if (t != null) {
				return t;
			} else {
				T o = (T) getSqlMapClientTemplate().queryForObject(
						cls.getSimpleName().toString() + ".upDesc", id);
				return o;
			}
		}
	}

	/**
	 * 修改排序
	 * 
	 * 
	 * @param id
	 *            NO
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PK upOrderNo(Map map) {
		PK id = (PK) getSqlMapClientTemplate().queryForObject(
				cls.getSimpleName().toString() + ".storedProcedure_order", map);
		return id;
	}
}
